﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CalcDataServ
{
    class ConfigEntry
    {
        public string Name;
        public object Value;

        public ConfigEntry(string Name, object Value)
        {
            this.Name = Name;
            this.Value = Value;
        }
    }

    class ConfigReader
    {
        private Dictionary<string, object> Engine;

        /// <summary>
        /// Get or set a config entry
        /// </summary>
        /// <param name="key">The name of the entry</param>
        /// <returns>The value associated ti the entry. If the key doesn't exist null is returned</returns>
        public object this[string key]
        {
            get
            {
                if (Engine.ContainsKey(key)) return Engine[key];
                else return null;
            }
            set
            {
                if (Engine.ContainsKey(key)) Engine[key] = value;
            }
        }

        /// <summary>
        /// Creates a new instance of the config reader
        /// </summary>
        public ConfigReader()
        {
            Engine = new Dictionary<string, object>();
            XmlDocument xml = new XmlDocument();
            XmlNode rootnode = null;
            bool ok = false;
            xml.Load("Configuration.xml");

            for (int i = 0; i < xml.ChildNodes.Count; i++)
            {
                if (xml.ChildNodes[i].Name == "Config")
                {
                    rootnode = xml.ChildNodes[i];
                    ok = true;
                    break;
                }
            }
            if (!ok) throw new Exception("Error reading configuration. Configuration file is invalid");

            ConfigEntry ent;
            foreach (XmlNode node in rootnode.ChildNodes)
            {
                ent = ParseNode(node);
                if (ent.Name != "Config") this.Engine.Add(ent.Name, ent.Value);
            }
        }

        private ConfigEntry ParseNode(XmlNode node)
        {
            if (node.LocalName != "#comment")
            {
                if (!string.IsNullOrEmpty(node.Attributes["type"].InnerText))
                {
                    switch (node.Attributes["type"].InnerText.ToLower())
                    {
                        case "int":
                            return new ConfigEntry(node.Name, Convert.ToInt32(node.InnerText));
                        case "bool":
                            return new ConfigEntry(node.Name, Convert.ToBoolean(node.InnerText));
                        case "string":
                            return new ConfigEntry(node.Name, node.InnerText);
                        case "stringlist":
                            string[] data = node.InnerText.ToLower().Split(';');
                            return new ConfigEntry(node.Name, data);
                        default:
                            throw new Exception("Error reading configuration. Configuration file is invalid");
                    }
                }
            }
            return new ConfigEntry("Config", null);
        }
    }
}
